package com.gs.SynAntiWandering.adapter;

import android.content.Context;
import android.widget.CheckBox;

import com.gs.SynAntiWandering.R;
import com.gs.SynAntiWandering.adapter.base.BaseAdapter;
import com.gs.SynAntiWandering.adapter.base.BaseViewHolder;
import com.gs.SynAntiWandering.db.dto.TagDto;

import java.util.List;

public class AddTagAdapter extends BaseAdapter<TagDto> {

    public AddTagAdapter(Context context, List<TagDto> mData, int layoutId) {
        super(context, mData, layoutId);
    }

    @Override
    public void convert(BaseViewHolder holder, TagDto bean, final int position) {
        CheckBox tvTagNum = holder.fd(R.id.cbxTag);
        tvTagNum.setText(bean.getTagNum());
        if (bean.getState() != null) {
            switch (bean.getState()) {
                case 0:
                    tvTagNum.setChecked(false);
                    break;
                case 1:
                    tvTagNum.setChecked(true);
                    break;
                case 2:
                    tvTagNum.setChecked(true);
                    tvTagNum.setEnabled(false);
                    tvTagNum.setText(bean.getTagNum() + " " + bean.getStateName());
                    break;
            }
        } else {
            tvTagNum.setChecked(false);
        }
        tvTagNum.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (list.get(position).getState() == null) {
                return;
            }
            if (list.get(position).getState() == 2) {
                return;
            }
            list.get(position).setState(isChecked ? 1 : 0);
        });
    }

    public void setAllCheck(boolean allCheck) {
        int state = allCheck ? 1 : 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getState() != 2) {
                list.get(i).setState(state);
            }
        }
        notifyDataSetChanged();
    }
}